package com.chhuang.ewcrm.ctrl;

import com.chhuang.ewcrm.api.AppRequest;
import com.chhuang.ewcrm.api.ServiceRxSchedulers;
import com.chhuang.ewcrm.api.ServiceSubscriber;
import com.chhuang.ewcrm.cons.Constants;
import com.chhuang.ewcrm.thread.AddLogThread;
import com.chhuang.ewcrm.utils.MyAlert;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author chhuang
 * @create 2020-10-31-10:48
 */
public class SelfPasswordController implements InitController{
    private static final Logger logger = LoggerFactory.getLogger(SelfPasswordController.class);

    @FXML
    private PasswordField oldPasswordPasswordField;
    @FXML
    private PasswordField passwordPasswordField;
    @FXML
    private PasswordField confirmPasswordPasswordField;
    @FXML
    private JFXButton saveJFXButton;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        saveJFXButton.setOnAction(event -> {
            String oldPassword = oldPasswordPasswordField.getText();
            if(StringUtils.isEmpty(oldPassword)){
                MyAlert.showError("错误","修改密码有误","请输入原密码");
                oldPasswordPasswordField.requestFocus();
                return;
            }
            String password = passwordPasswordField.getText();
            if(StringUtils.isEmpty(password)){
                MyAlert.showError("错误","修改密码有误","请输入新密码");
                passwordPasswordField.requestFocus();
                return;
            }
            String confirmPassword = confirmPasswordPasswordField.getText();
            if(StringUtils.isEmpty(confirmPassword)){
                MyAlert.showError("错误","修改密码有误","请输入确认密码");
                confirmPasswordPasswordField.requestFocus();
                return;
            }
            if(!password.equals(confirmPassword)){
                MyAlert.showError("错误","修改密码有误","新密码与确认密码不一致");
                passwordPasswordField.requestFocus();
                return;
            }
            AppRequest.getInstance().getService().updateUserPassword(oldPassword,password,confirmPassword)
                    .compose(ServiceRxSchedulers.compose())
                    .subscribe(new ServiceSubscriber<>("修改用户密码") {
                        @Override
                        protected void onHandleSuccess(Void aVoid) {
                            new Thread(new AddLogThread(Constants.loginUser.getUserId(),
                                    Constants.loginUser.getUsername(),
                                    Constants.loginUser.getFullname(),
                                    "修改个人登录密码成功")).start();
                            MyAlert.showAlertLater("成功","修改用户密码完成","");
                        }
                    });
        });
    }

    @Override
    public void close() {

    }
}
